package com.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.content.model.dto.AssociationMediaDto;
import com.content.model.dto.SaveTeachplanDto;
import com.content.model.dto.TeachPlanTreeDto;
import com.content.model.po.Teachplan;
import com.content.model.po.TeachplanMedia;
import com.content.service.TeachPlanService;
import com.content.service.mapper.TeachplanMapper;
import com.content.service.mapper.TeachplanMediaMapper;
import com.content.base.exception.XueChengPlusException;
import com.media.po.MediaFiles;
import com.media.service.MediaFilesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
//6666
@Service
public class TeachPlanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachPlanService {
    @Autowired
    private TeachplanMapper teachplanMapper;
    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachPlanTreeDto> getTeachPlanByCourseId(Long courseId) {
        List<TeachPlanTreeDto> teachPlanByCourseId = teachplanMapper.getTeachPlanByCourseId(courseId);

        return teachPlanByCourseId;
    }

    /**
     * 保存或更新课程计划
     *
     * @param saveTeachplanDto
     */
    @Override
    @Transactional
    public void saveTeachPlan(SaveTeachplanDto saveTeachplanDto) {
        Teachplan teachplan = new Teachplan();
        BeanUtils.copyProperties(saveTeachplanDto, teachplan);
        if (saveTeachplanDto.getId() != null) {  //看前端是否传id
            teachplanMapper.updateById(teachplan);
        } else {
            teachplanMapper.insert(teachplan);
        }
    }

    /**
     * 课程计划下移
     *
     * @param id
     */
    @Override
    @Transactional
    public void moveDown(Long id) {
        //根据课程计划的id找到当前记录的父id
        Teachplan teachplan = teachplanMapper.selectById(id);
        Long parentid = teachplan.getParentid();
        List<Teachplan> list = lambdaQuery().eq(Teachplan::getParentid, parentid).orderByAsc(Teachplan::getOrderby).list();
        //将当前记录的排序字段与其下一个相邻的节点进行交换
        int size = list.size();

        list.forEach(t -> {
            if (t.getId().equals(id)) {
                int index = list.indexOf(t);
                if(index + 1 < size){
                    Integer orderby = t.getOrderby();
                    Teachplan teachplan1 = list.get(index + 1);
                    if (teachplan1 != null) {
                        Integer orderby1 = teachplan1.getOrderby();
                        teachplan1.setOrderby(orderby);
                        t.setOrderby(orderby1);
                    }
                    updateById(t);
                    updateById(teachplan1);
                }else {
                    XueChengPlusException.cast("当前课程不可下移");
                }
            }
        });
    }

    /**
     * 课程计划上移
     * @param id
     */
    @Override
    @Transactional
    public void moveUp(Long id) {
        //根据课程计划的id找到当前记录的父id
        Teachplan teachplan = teachplanMapper.selectById(id);
        Long parentid = teachplan.getParentid();
        List<Teachplan> list = lambdaQuery().eq(Teachplan::getParentid, parentid).orderByAsc(Teachplan::getOrderby).list();
        //将当前记录的排序字段与其下一个相邻的节点进行交换

        list.forEach(t -> {
            if (t.getId().equals(id)) {
                int index = list.indexOf(t);
                if(index - 1 >= 0){
                    Integer orderby = t.getOrderby();
                    Teachplan teachplan1 = list.get(index - 1);
                    if (teachplan1 != null) {
                        Integer orderby1 = teachplan1.getOrderby();
                        teachplan1.setOrderby(orderby);
                        t.setOrderby(orderby1);
                    }
                    updateById(t);
                    updateById(teachplan1);
                }else {
                    XueChengPlusException.cast("当前课程不可上移");
                }
            }
        });
    }

    /**
     * 删除课程计划
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Long id) {
        //删除大章节，如果当前节点还有子节点提示有子节点，无法删除
        List<Teachplan> list = lambdaQuery().eq(Teachplan::getParentid, id).list();
        if (!list.isEmpty()) {
            XueChengPlusException.cast("当前章节下有子课程计划，无法删除");
        }

        teachplanMapper.deleteById(id);
        //删除小章节同时将关联的信息进行删除
        teachplanMediaMapper.deleteByTeachPlanId(id,null);
    }

    /**
     * 删除课程计划中媒资信息
     * @param teachPlanId
     * @param mediaId
     */
    @Override
    @Transactional
    public void deleteMedia(Long teachPlanId, String mediaId) {
        teachplanMediaMapper.deleteByTeachPlanId(teachPlanId,mediaId);
    }

    @Override
    @Transactional
    public void associationMedia(AssociationMediaDto associationMediaDto) {
        //先删除原有记录，根据课程计划id删除它所绑定的媒资
        //匿名内部类不像直接创建对象可以省略new后面的泛型类型，这里要将泛型类型体现出来
        teachplanMediaMapper.delete(new LambdaUpdateWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId,associationMediaDto.getTeachplanId()));

        //获取媒资信息原始文件名
        TeachplanMedia teachplanMedia = new TeachplanMedia();

        teachplanMedia.setMediaId(associationMediaDto.getMediaId());
        teachplanMedia.setMediaFilename(associationMediaDto.getFileName());
        teachplanMedia.setTeachplanId(associationMediaDto.getTeachplanId());

        //根据teachplanId查询课程计划
        Teachplan teachplan = teachplanMapper.selectById(associationMediaDto.getTeachplanId());

        if (teachplan == null) {
            XueChengPlusException.cast("课程计划不存在！");
        }
        if(teachplan.getGrade() != 2){
            XueChengPlusException.cast("课程计划不是小节，不能绑定媒资！");
        }
        teachplanMedia.setCourseId(teachplan.getCourseId());

        try {
            //保存至课程计划媒资信息关联表
            teachplanMediaMapper.insert(teachplanMedia);
        }catch (Exception e){
            XueChengPlusException.cast("课程计划及媒资关联信息失败！");
        }

    }
}
